﻿using FDEE.Model;
using FDEEDrawingModels;
using FDEEPublicFunc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FDEEMain
{
    public partial class SelectParamentForm : Form
    {
        FDEEReber fdeeReber;
        public InitTypePara para = new InitTypePara();
        List<ParaItem> items;
        private List<Control> _listParamCtrl = new List<Control>(); // 动态创建控件

        List<string> _ListSubDes = new List<string>() {
        "墩高7.00-10.00", "墩高10.01-15.00","墩高15.01-20.00","墩高20.01-25.00"};
        private double _scaleX = 1;
        private double _scaleY = 1;
        public SelectParamentForm()
        {
            InitializeComponent();
            using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
            {
                float dpiX = graphics.DpiX;
                float dpiY = graphics.DpiY;

                _scaleX += (dpiX - 96) * 1.041666 / 100;
                _scaleY += (dpiY - 96) * 1.041666 / 100;
            }
            InitForm();
            para = new InitTypePara();
        }
        private void InitForm()
        {
            var values = Enum.GetValues(typeof(FDEE.Model.eSubType));
            int index = 0;
            foreach (var item in values)
            {
                cbHeight.Items.Add(item);
                ++index;
            }

            cbType.SelectedIndex = 0;
            if(cbHeight.Items.Count > 2)
            {
                cbHeight.SelectedIndex = 1;
            }
        }

        private void btnConfim_Click(object sender, EventArgs e)
        {
            para.eType = (EStructure)cbType.SelectedIndex;          // 构造物类型
            para.eSubType = ((CBItem)cbSubType.SelectedItem).tag;                // 构造物子类型（枚举值）
            
            para.type = cbSubType.Text;                             // 构造物子类型（字符串）
            para.eHeightType = (FDEE.Model.eSubType)cbHeight.SelectedIndex; // 墩高分类
            para.nHeightType = cbHeight.SelectedIndex;
            List<ValueType> valueTypes = new List<ValueType>();
            foreach (Control ctrl in panelPara.Controls)
            {
                string type = ctrl.GetType().ToString();
                if (ctrl.GetType().ToString() == "System.Windows.Forms.TextBox" || ctrl.GetType().ToString() == "combo")
                {
                    ValueType item = new ValueType();
                    item.value = ctrl.Text;
                    item.valueType = "string";
                    valueTypes.Add(item);
                }
            }
            para.paras = valueTypes;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// 创建文本和数字输入框
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="type"></param>
        private void createText(List<ParaItem> items)
        {
            panelPara.Controls.Clear();
            int rowNum = 0;
            Point lablePt = label1.Location;
            foreach (ParaItem item in items)
            {
                rowNum++;
                string unit = "";
             
                Label lbTxt = new Label();//字段名称
                lbTxt.TextAlign = ContentAlignment.MiddleLeft;
                Graphics graphics1 = CreateGraphics();
                SizeF sizeF = graphics1.MeasureString(item.Name, lbTxt.Font);
                float strwidth = sizeF.Width;
                lbTxt.Tag = item.Name;
              
                lbTxt.Width = (int)strwidth + 9;
                lbTxt.Height = 21;
                if (60 - (int)strwidth > 0)
                {
                    lbTxt.Left = 61 - (int)strwidth;
                }
                else
                {
                    lbTxt.Left = 10;
                }

                string fieldname = item.Name;
                if (lbTxt.Width > 60)
                {
                    int f = (int)strwidth / item.Name.Length;
                    int max = 60 / f;
                    fieldname = item.Name.Substring(0, max);
                    lbTxt.MouseHover += LbTxt_MouseHover;
                }
                lbTxt.Text = fieldname + ":";
                //lbTxt.Left = 70 - (int)strwidth - 10;
                //lbTxt.Top = (rowNum - 1) * 23;
                if(rowNum == 1)
                {
                    lbTxt.Location = new Point((int)((lablePt.X+ label1.Width/2)/**_scaleX*/), (int)(lablePt.Y/**_scaleY*/));
                }
                else
                {
                    
                    lbTxt.Location = new Point((int)(_listParamCtrl[_listParamCtrl .Count- 1].Location.X/**_scaleX*/), 
                        (int)((_listParamCtrl[_listParamCtrl.Count - 1].Location.Y + _listParamCtrl[_listParamCtrl.Count - 1].Height + 20)/**_scaleY*/));
                }
                lbTxt.Size = new Size((int)(45*_scaleX),(int)(15*_scaleY));
                panelPara.Controls.Add(lbTxt);

                _listParamCtrl.Add(lbTxt);

                if(!string.IsNullOrEmpty(item.Values))
                {
                    CreateControlByType(lbTxt, item.Key, item.Key, new List<string>(item.Values.Split(',')));
                }
                else
                {
                    CreateControlByType(lbTxt, item.Key, item.Key, null);
                }
            }
            this.Height = (int)((155 + rowNum * 30)*_scaleX);
        }

        private void CreateControlByType(Label lbTxt, string type, string key, List<string> values)
        {
            int width = cbType.Width;
            int height = cbType.Height;
            if (values == null || values.Count == 0)
            {
                // 编辑框
                TextBox txt = new TextBox();
                txt.Name = key;
                txt.Tag = type;
                txt.Size = new Size(width, height);
                txt.Location = new Point(lbTxt.Location.X + lbTxt.Width, lbTxt.Location.Y - 5);
                panelPara.Controls.Add(txt);
            }
            else
            {
                // 下拉框
                ComboBox txt = new ComboBox();
                txt.SelectedIndexChanged += ComboxEventHandler;
                txt.Name = key;
                txt.DropDownStyle = ComboBoxStyle.DropDownList;
                txt.Tag = type;
                txt.Size = new Size(width, height);
                txt.Location = new Point(lbTxt.Location.X + lbTxt.Width, lbTxt.Location.Y - 5);
                panelPara.Controls.Add(txt);

                foreach (string str in values)
                {
                    txt.Items.Add(str);
                }
            }
        }

        private void LbTxt_MouseHover(object sender, EventArgs e)
        {
            try
            {
                Label lb = sender as Label;
                toolTip.SetToolTip(lb, lb.Tag.ToString());
            }
            catch (Exception ex)
            {
                
            }
        }

        private void cbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 创建输入参数控件
            List<ParaItem> items = new List<ParaItem>();
            cbSubType.Items.Clear();

            switch (cbType.SelectedIndex)
            {
                case 0:             // 桩基
                    {
                        cbHeight.Visible = true;
                        lb.Visible = true;
                        desLabel.Visible = true;
                        cbSubType.DisplayMember = "name";
                        var values = Enum.GetValues(typeof(FDEE.Model.EPileType));
                        foreach (var item in values)
                        {
                            CBItem data = new CBItem();
                            int tag = (int)item;
                            data.name = item.ToString();
                            data.tag = tag;
                            cbSubType.Items.Add(data);
                        }

                        ParaItem para = new ParaItem();
                        para.Name = "桩长";
                        para.ValueType = "double";
                        items.Add(para);
                        para = new ParaItem();
                        para.Name = "DOWEL";
                        para.ValueType = "double";
                        items.Add(para);
                        break;
                    }
                case 1:             // 承台
                    {
                        cbHeight.Visible = true;
                        lb.Visible = true;
                        desLabel.Visible = true;
                        cbSubType.DisplayMember = "name";
                        var values = Enum.GetValues(typeof(FDEE.Model.EFootingType));
                        foreach (var item in values)
                        {
                            CBItem data = new CBItem();
                            int tag = (int)item;
                            data.name = item.ToString();
                            data.tag = tag;
                            cbSubType.Items.Add(data);
                        }

                        break;
                    }
                case 2:             // 墩柱
                    {
                        cbHeight.Visible = true;
                        lb.Visible = true;
                        desLabel.Visible = true;
                        cbSubType.DisplayMember = "name";
                        var values = Enum.GetValues(typeof(FDEE.Model.eColumnType));
                        foreach (var item in values)
                        {
                            CBItem data = new CBItem();
                            int tag = (int)item;
                            data.name = item.ToString();
                            data.tag = tag;
                            cbSubType.Items.Add(data);
                        }

                        ParaItem para = new ParaItem();
                        para.Name = "墩高";
                        para.ValueType = "int";
                        items.Add(para);
                        ParaItem para2 = new ParaItem();
                        para2.Name = "墩号";
                        para2.ValueType = "string";
                        items.Add(para2);
                        break;
                    }
                case 3:             // 盖梁
                    {
                        cbHeight.Visible = false;
                        lb.Visible = false;
                        desLabel.Visible = false;
                        cbSubType.DisplayMember = "name";
                        var values = Enum.GetValues(typeof(FDEE.Model.ECrossBeamType));
                        foreach (var item in values)
                        {
                            CBItem data = new CBItem();
                            int tag = (int)item;
                            data.name = item.ToString();
                            data.tag = tag;
                            cbSubType.Items.Add(data);
                        }

                        ParaItem para1 = new ParaItem();
                        para1.Name = "宽度";
                        para1.ValueType = "double";
                        items.Add(para1);

                        ParaItem para2 = new ParaItem();
                        para2.Name = "左边宽度";
                        para2.ValueType = "double";
                        items.Add(para2);
                        break;
                    }
                default:
                    break;
            }

            if(cbSubType.Items.Count > 2)
            {
                cbSubType.SelectedIndex = 1;
            }

            createText(items);
        }

        public void ComboxEventHandler(object sender, EventArgs e)
        {
            ComboBox combo = sender as ComboBox;
            string str = combo.Name;
        }

        private void cbHeight_SelectedIndexChanged(object sender, EventArgs e)
        {
            string TypeName = cbSubType.Text;
            FDEE.Model.eSubType type = (FDEE.Model.eSubType)cbHeight.SelectedIndex;
            string des = "";
            try
            {
                des = Common.GetPierHeightDes(TypeName, type);
            }
            catch(Exception ex) { }
            desLabel.Text = des;
        }

        private void cbSubType_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbHeight_SelectedIndexChanged(null, null);
        }
    }
    public class InitTypePara
    {
        public string type { get; set; }                        // 构造物类型（字符串）
        public EStructure eType { get; set; }                   // 构造物类型（枚举）
        public int eSubType { get; set; }                       // 构造物子类型
        public FDEE.Model.eSubType eHeightType { get; set; }    // 构造物墩高分类
        public int nHeightType { get; set; }

        public List<ValueType> paras { get; set; }              // 参数
    }

    public class ValueType
    {
        public string value { get; set; }

        public string valueType { get; set; }
    }

    public class CBItem
    {
        public string name { get; set; }
        public int tag { get; set; }
    }
}
